//
//  WJCameraViewController.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/26.
//

import UIKit
import OpenScanner

class WJCameraViewController: WJBaseViewViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let scanner = VisionView(self.view.frame)
        scanner.delegate = self
        self.view.addSubview(scanner)
        scanner.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
        }
    }
}

extension WJCameraViewController: OpenScannerDelegate {
    func openScanner(_ documentImages: [UIImage]) {
        WJPdfGenerator.createPDFAndSaveToDatabase(from: documentImages, open: false) { [weak self] success in
            guard let self = self else { return }
            if !success {
                WJProgressHUD.showInfo(with: "Failed")
            }
            dismiss(animated: true)
        }
    }
    
    func openScannerDidPressCancel() {
        dismiss(animated: true)
    }
    
    func openScanner(_ didFailWithError: Error) {
        dismiss(animated: true)
    }
}
